home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 January / january_2001.iso / intercd / root / ^4Developers / VB / visbasdb / VBDB / VBDB Code / VB6 Code / Class 6 / Example6-8DE.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-09-17  |  8.9 KB  |  315 lines

  1. VERSION 5.00
  2. Begin VB.Form frmAuthors 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Authors"
  5.    ClientHeight    =   3060
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   4680
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   3060
  13.    ScaleWidth      =   4680
  14.    StartUpPosition =   3  'Windows Default
  15.    Begin VB.CommandButton cmdHelp 
  16.       Caption         =   "&Help"
  17.       Height          =   375
  18.       Left            =   3240
  19.       TabIndex        =   14
  20.       TabStop         =   0   'False
  21.       Top             =   2640
  22.       Width           =   1215
  23.    End
  24.    Begin VB.CommandButton cmdDone 
  25.       Caption         =   "Do&ne"
  26.       Height          =   375
  27.       Left            =   3240
  28.       TabIndex        =   13
  29.       TabStop         =   0   'False
  30.       Top             =   2160
  31.       Width           =   1215
  32.    End
  33.    Begin VB.CommandButton cmdDelete 
  34.       Caption         =   "&Delete"
  35.       Height          =   375
  36.       Left            =   1680
  37.       TabIndex        =   12
  38.       TabStop         =   0   'False
  39.       Top             =   2160
  40.       Width           =   1215
  41.    End
  42.    Begin VB.CommandButton cmdEdit 
  43.       Caption         =   "&Edit"
  44.       Height          =   375
  45.       Left            =   120
  46.       TabIndex        =   11
  47.       TabStop         =   0   'False
  48.       Top             =   1680
  49.       Width           =   1215
  50.    End
  51.    Begin VB.CommandButton cmdCancel 
  52.       Caption         =   "&Cancel"
  53.       Height          =   375
  54.       Left            =   3240
  55.       TabIndex        =   10
  56.       TabStop         =   0   'False
  57.       Top             =   1680
  58.       Width           =   1215
  59.    End
  60.    Begin VB.CommandButton cmdSave 
  61.       Caption         =   "&Save"
  62.       Height          =   375
  63.       Left            =   1680
  64.       TabIndex        =   9
  65.       TabStop         =   0   'False
  66.       Top             =   1680
  67.       Width           =   1215
  68.    End
  69.    Begin VB.CommandButton cmdAddNew 
  70.       Caption         =   "&Add New"
  71.       Height          =   375
  72.       Left            =   120
  73.       TabIndex        =   8
  74.       TabStop         =   0   'False
  75.       Top             =   2160
  76.       Width           =   1215
  77.    End
  78.    Begin VB.CommandButton cmdNext 
  79.       Caption         =   "Next =>"
  80.       Height          =   255
  81.       Left            =   2520
  82.       TabIndex        =   7
  83.       TabStop         =   0   'False
  84.       Top             =   1320
  85.       Width           =   1215
  86.    End
  87.    Begin VB.CommandButton cmdPrevious 
  88.       Caption         =   "<= Previous"
  89.       Height          =   255
  90.       Left            =   1080
  91.       TabIndex        =   6
  92.       TabStop         =   0   'False
  93.       Top             =   1320
  94.       Width           =   1215
  95.    End
  96.    Begin VB.TextBox txtYearBorn 
  97.       DataField       =   "Year Born"
  98.       DataMember      =   "comAuthors"
  99.       DataSource      =   "denBooks"
  100.       Height          =   285
  101.       Left            =   1200
  102.       Locked          =   -1  'True
  103.       MaxLength       =   4
  104.       TabIndex        =   2
  105.       Text            =   "Text"
  106.       Top             =   840
  107.       Width           =   1215
  108.    End
  109.    Begin VB.TextBox txtAuthor 
  110.       DataField       =   "Author"
  111.       DataMember      =   "comAuthors"
  112.       DataSource      =   "denBooks"
  113.       Height          =   285
  114.       Left            =   1200
  115.       Locked          =   -1  'True
  116.       TabIndex        =   1
  117.       Text            =   "Text2"
  118.       Top             =   480
  119.       Width           =   3375
  120.    End
  121.    Begin VB.TextBox txtAuthorID 
  122.       DataField       =   "Au_ID"
  123.       DataMember      =   "comAuthors"
  124.       DataSource      =   "denBooks"
  125.       Height          =   285
  126.       Left            =   1200
  127.       Locked          =   -1  'True
  128.       TabIndex        =   3
  129.       TabStop         =   0   'False
  130.       Text            =   "Text1"
  131.       Top             =   120
  132.       Width           =   1215
  133.    End
  134.    Begin VB.Label Label3 
  135.       Caption         =   "Year Born"
  136.       Height          =   255
  137.       Left            =   120
  138.       TabIndex        =   5
  139.       Top             =   840
  140.       Width           =   1215
  141.    End
  142.    Begin VB.Label Label2 
  143.       Caption         =   "Author Name"
  144.       Height          =   255
  145.       Left            =   120
  146.       TabIndex        =   4
  147.       Top             =   480
  148.       Width           =   1215
  149.    End
  150.    Begin VB.Label Label1 
  151.       Caption         =   "Author ID"
  152.       Height          =   255
  153.       Left            =   120
  154.       TabIndex        =   0
  155.       Top             =   120
  156.       Width           =   1215
  157.    End
  158. Attribute VB_Name = "frmAuthors"
  159. Attribute VB_GlobalNameSpace = False
  160. Attribute VB_Creatable = False
  161. Attribute VB_PredeclaredId = True
  162. Attribute VB_Exposed = False
  163. Option Explicit
  164. Private Declare Function WinHelp Lib "user32" Alias "WinHelpA" (ByVal hwnd As Long, ByVal lpHelpFile As String, ByVal wCommand As Long, ByVal dwData As Long) As Long
  165. Private Const HELP_FINDER = &HB&
  166. Private Sub ValidateData(AllOK As Boolean)
  167. Dim Message As String
  168. Dim InputYear As Integer, CurrentYear As Integer
  169. AllOK = True
  170. Message = ""
  171. 'Check for name
  172. If Len(txtAuthor.Text) = 0 Then
  173.   Message = "You must enter an Author Name." + vbCrLf
  174.   txtAuthor.SetFocus
  175.   AllOK = False
  176. End If
  177. 'Check length and range on Year Born
  178. InputYear = Val(txtYearBorn.Text)
  179. CurrentYear = Val(Format(Now, "yyyy"))
  180. If Len(txtYearBorn.Text) <> 0 Then
  181.   If InputYear > CurrentYear Or InputYear < CurrentYear - 150 Then
  182.     Message = Message + "Year Born must be between" & Str(CurrentYear - 150) & " and" & Str(CurrentYear) & "."
  183.     txtYearBorn.SetFocus
  184.     AllOK = False
  185.   End If
  186. End If
  187. If Not (AllOK) Then
  188.   MsgBox Message, vbOKOnly + vbInformation, "Validation Error"
  189. End If
  190. End Sub
  191. Private Sub cmdAddNew_Click()
  192. On Error GoTo HandleErrors
  193. Call SetState("Add")
  194. Exit Sub
  195. HandleErrors:
  196. Select Case MsgBox(Err.Description, vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number) + " in " + Err.Source)
  197. Case vbAbort
  198.   Exit Sub
  199. Case vbRetry
  200.   Resume
  201. Case vbIgnore
  202.   Resume Next
  203. End Select
  204. End Sub
  205. Private Sub cmdCancel_Click()
  206. Call SetState("View")
  207. End Sub
  208. Private Sub cmdDelete_Click()
  209. Dim Response As Integer
  210. On Error GoTo HandleErrors
  211. Response = MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion + vbDefaultButton2, "Delete")
  212. If Response = vbNo Then
  213.   Exit Sub
  214. End If
  215. Exit Sub
  216. HandleErrors:
  217. Select Case MsgBox(Err.Description, vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number) + " in " + Err.Source)
  218. Case vbAbort
  219.   Exit Sub
  220. Case vbRetry
  221.   Resume
  222. Case vbIgnore
  223.   Resume Next
  224. End Select
  225. End Sub
  226. Private Sub cmdEdit_Click()
  227. Call SetState("Edit")
  228. End Sub
  229. Private Sub cmdHelp_Click()
  230. Dim Rtn As Long
  231. Rtn = WinHelp(frmAuthors.hwnd, App.HelpFile, HELP_FINDER, CLng(0))
  232. End Sub
  233. Private Sub cmdNext_Click()
  234. denBooks.rscomAuthors.MoveNext
  235. If denBooks.rscomAuthors.EOF Then
  236.   Beep
  237.   denBooks.rscomAuthors.MoveLast
  238. End If
  239. End Sub
  240. Private Sub cmdPrevious_Click()
  241. denBooks.rscomAuthors.MovePrevious
  242. If denBooks.rscomAuthors.BOF Then
  243.   Beep
  244.   denBooks.rscomAuthors.MoveFirst
  245. End If
  246. End Sub
  247. Private Sub cmdSave_Click()
  248. Dim Valid As Boolean
  249. Call ValidateData(Valid)
  250. If Not (Valid) Then Exit Sub
  251. On Error GoTo HandleErrors
  252. MsgBox "Record saved.", vbOKOnly + vbInformation, "Save"
  253. Call SetState("View")
  254. Exit Sub
  255. HandleErrors:
  256. Select Case MsgBox(Err.Description, vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number) + " in " + Err.Source)
  257. Case vbAbort
  258.   Exit Sub
  259. Case vbRetry
  260.   Resume
  261. Case vbIgnore
  262.   Resume Next
  263. End Select
  264. End Sub
  265. Private Sub SetState(AppState As String)
  266. Select Case AppState
  267. Case "View"
  268.   txtAuthorID.BackColor = vbWhite
  269.   txtAuthor.Locked = True
  270.   txtYearBorn.Locked = True
  271.   cmdPrevious.Enabled = True
  272.   cmdNext.Enabled = True
  273.   cmdAddNew.Enabled = True
  274.   cmdSave.Enabled = False
  275.   cmdCancel.Enabled = False
  276.   cmdEdit.Enabled = True
  277.   cmdDelete.Enabled = True
  278.   cmdDone.Enabled = True
  279.   txtAuthor.SetFocus
  280. Case "Add", "Edit"
  281.   txtAuthorID.BackColor = vbRed
  282.   txtAuthor.Locked = False
  283.   txtYearBorn.Locked = False
  284.   cmdPrevious.Enabled = False
  285.   cmdNext.Enabled = False
  286.   cmdAddNew.Enabled = False
  287.   cmdSave.Enabled = True
  288.   cmdCancel.Enabled = True
  289.   cmdEdit.Enabled = False
  290.   cmdDelete.Enabled = False
  291.   cmdDone.Enabled = False
  292.   txtAuthor.SetFocus
  293. End Select
  294. End Sub
  295. Private Sub Form_Activate()
  296. Call SetState("View")
  297. End Sub
  298. Private Sub txtAuthor_KeyPress(KeyAscii As Integer)
  299. If KeyAscii = vbKeyReturn Then
  300.   txtYearBorn.SetFocus
  301.   Exit Sub
  302. End If
  303. End Sub
  304. Private Sub txtYearBorn_KeyPress(KeyAscii As Integer)
  305. If KeyAscii = vbKeyReturn Then
  306.   txtAuthor.SetFocus
  307.   Exit Sub
  308. End If
  309. If (KeyAscii >= Asc("0") And KeyAscii <= Asc("9")) Or KeyAscii = vbKeyBack Then
  310.   Exit Sub
  311.   Beep
  312.   KeyAscii = 0
  313. End If
  314. End Sub
  315.